package com.bdqn.tour.servlet.comments;

import com.bdqn.tour.dao.HotelImplementationClass;
import com.bdqn.tour.dao.impl.ImplUersDao;

import com.bdqn.tour.entity.Comment;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet("/indextom")
public class HTmlEE extends HttpServlet {
    private static final HotelImplementationClass uerdao = new ImplUersDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");

        // 检查请求是否为多部分表单数据
        if (!ServletFileUpload.isMultipartContent(req)) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "请求不是多部分表单数据");
            return;
        }

        final String UPLOAD_DIR = "uploads";
        String uploadPath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIR;
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        String uploadDirParam = null;
        String comments = null;
        String xi = null;
        String name = null;
        String filePath = null;

        try {
            List<FileItem> items = upload.parseRequest(req);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    // 处理表单字段
                    String fieldName = item.getFieldName();
                    String fieldValue = item.getString("UTF-8");
                    switch (fieldName) {
                        case "up":
                            uploadDirParam = fieldValue;
                            break;
                        case "comments":
                            comments = fieldValue;
                            break;
                        case "hobby":
                            xi = fieldValue;
                            break;
                        case "uer":
                            name = fieldValue;
                            break;
                    }
                } else {
                    // 处理文件字段
                    String fileName = UUID.randomUUID().toString() + "_" + item.getName();
                    File uploadedFile = new File(uploadPath, fileName);
                    item.write(uploadedFile);

                    // 存储文件路径
                    filePath = UPLOAD_DIR + File.separator + fileName;
                }
            }

            // 创建 Comment 对象并设置属性
            Comment users = new Comment();
            users.setUsercomment(comments);
//            users.setUserId(uerdao.getUserIdByUsername(name));
            users.setXi(xi);
            users.setImg(filePath);

            // 添加用户评论到数据库
            uerdao.addUsers(users);

            resp.getWriter().println("评论和图片上传成功！");
        } catch (Exception e) {
            e.printStackTrace();
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "评论和图片上传失败: " + e.getMessage());
        }
    }
}
